/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kallsonys.session;

import com.kallsonys.object.OrderOms;
import com.kallsonys.object.Orderitem;
import com.kallsonys.object.Product;
import java.math.BigDecimal;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author alladiab
 */
@Stateless
public class OrderitemFacade extends AbstractFacade<Orderitem> {
    @PersistenceContext(unitName = "Kallsonys-WarPU")
    private EntityManager em;

    protected EntityManager getEntityManager() {
        return em;
    }

    public OrderitemFacade() {
        super(Orderitem.class);
    }
    
    public List<Orderitem> getByOrderId( long idOrder )
    {
         Query queryfindByProductidproduct = em.createNamedQuery("Orderitem.findByIdOrder");
         queryfindByProductidproduct.setParameter("orderid", new OrderOms(idOrder));
         
         List<Orderitem> lstItems = (List<Orderitem>)queryfindByProductidproduct.getResultList();
         
         for ( Orderitem item : lstItems )
         {
             queryfindByProductidproduct = em.createNamedQuery("Product.findByProductid");
             queryfindByProductidproduct.setParameter("productid",  BigDecimal.valueOf(item.getProductid()));
             Product p = (Product)queryfindByProductidproduct.getSingleResult();
             item.setProductName(p.toString());
         }
         
         return lstItems;
    }
    
   
    
}
